#################################################
#################################################
####### PyMed, Wed 14th March 2012
####### Released under the GPL version 3
####### Matt Williams & Tony Hunter
#################################################

#Set options here: 

prefcriterion = "prefcriterion1"
metarules = ["nonStatSig"]
filename = 'Lung_ChemoRT_M0.5_Cochrane_Base.csv'

#filename = 'Lung_ChemoRT_M0.5_Cochrane_Base_RTChemo_Relax.csv'
#filename = 'Lung_ChemoRT_M0.5_Cochrane_Ext_RTChemo_Relax.csv'
#filename = 'Lung_ChemoRT_Merged_0.5_RTChemo_Relax.csv'

# Options for preference criteria(i.e. for choosing which attacks hold between inductive arguments)are:
# "prefcriterion1", "prefcriterion2", "prefcriterion3"
# The first counts all outcomes, the second counts all non-tox outcomes, and the third prefers args without death tox rate > 1%, and then for the remainder counts all outcomes

# Choice of metarules for generating meta-arguments is given by the metarules parameter (a list of rule names)
# For rule name in the list, the metarule will be used to generate meta-arguments.
#
# Choices are:
#
# - nonEuropean (i.e. some evidence is from non European country)
# - SEAsian (i.e. some evidence is from SE Asian country)
# - US (i.e. some evidence is from US)
# - nonStatSig (i.e. some evidence is not statistically significant)
# - induction (i.e. for pro argument, when left treatment uses induction,
#              and for con argument, when right treatment uses induction).
#
# Zero or more choices can appear in the metarules list - e.g: 

#metarules = ["US"]
#metarules = ["US","induction"]
#metarules = ["induction"]
#metarules = ["nonStatSig"]
#metarules = ["PhaseII"]
#metarules = ["StageII"]

#The filename should be the file with data on trials to use as input: it will assume that the file is in the "Input" directory. E.g.:

#filename = 'Lung_ChemoRT_M0.5_Cochrane_Base_RTChemo_Relax.csv'
#filename = 'Lung_ChemoRT_M0.5_Cochrane_Ext_RTChemo_Relax.csv'
#filename = 'Lung_ChemoRT_Merged_0.5_RTChemo_Relax.csv'


###############################################################################
###############################################################################
### Below here, actual program starts
###############################################################################
import generateTable
import expandTable
import queryTable
import normalizeTable
import generateSuperGraph
import outputFunctions
import os
import subprocess
import utilityFunctions


#################################################
#################################################

print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "\n"
print "PYMED - Thursday 14th March 2012"

print "\n"
print "\n"


#outputdirectory = "../Output"
inputdirectory = '../Input/'


inputFileName = inputdirectory + filename

OutputName = filename
outputFile = "output.html"
dotOutputName = OutputName + '.dot'

print "Read following spreadsheet"

print inputFileName

evdb = generateTable.getmydb(inputFileName)

evdb = expandTable.addriskratio(evdb)

#for x in evdb:
#    print x

#mykeys = evdb[0].keys()

#for c in mykeys:
#    print c


print "\n"
print "\n"
print "Using following rows from spreadsheet (possibly with offset of 1)"

print queryTable.getmyidlist(evdb)

print "\n"
print "\n"
print "Treatments appearing in spreadsheet"

treatments = queryTable.getmyoplist(evdb)

for x in treatments:
    print x

print "\n"
print "\n"

for x in evdb:
    print x['myid']

print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"

print "\n"
print "\n"
print "Start superiority graph construction"
print "\n"
print "\n"


print "\n"
print "Treatment pairs that appear in spreadsheet"
print "(i.e. there is at least one row in the"
print "spreadsheet comparing the treatments)"
print "\n"

print "Call queryTable.getmyoppairs"


treatmentpairs = queryTable.getmyoppairs(evdb)

for p in treatmentpairs:
    print p

print "\n"

print "Call generateSuperGraph.filterpairs"

selectedpairs = generateSuperGraph.filterpairs(treatmentpairs,evdb)

for p in selectedpairs:
    print p
    
print "\n"
print "\n"

print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"


superResults = generateSuperGraph.makeSuperGraph(selectedpairs,evdb,prefcriterion,metarules)

print "\n"
print "\n"

print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"


print "\n"
print "Super graph arcs"
print "\n"

for x in superResults:
    print x

print "\n"
print "This superiority graph was constructed using"
print "the following preference criterion over inductive"
print "arguments (for determining the strict attack"
print "relation."
print "\n"
print prefcriterion
print "\n"
print "and the following meta rules used for generating"
print "the meta-arguments"
print "\n"
print metarules
print "\n"

print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "\n"
print "Use the following as a dot file with GVedit"
print "\n"



###########################################
###########################################
#From here we manage the output
#dir = os.getcwd()
#print "Using dir: ", dir, " as a base."

outputDirectory = "../Output/"

#print "\n"

####These make the top-level files: A normalised evidence table and a picture
#newOutDir = str(dir) + '\\' + dotOutputName.rstrip('.dot') + '_dir'
subOutDir = outputDirectory  + OutputName + '_dir' + '/'

print "Using dir: ", outputDirectory, " as a base."
print "Using dir: ", subOutDir, " as a subDir"

utilityFunctions.ensure_dir(subOutDir)

###We've made a sub-directory, so now fill it with subtables
#print "SuperRes: ", len(superResults), superResults
outputFunctions.generateSubtables(evdb, selectedpairs, superResults, subOutDir,metarules,prefcriterion)
outputFunctions.generateDotFile(inputFileName, dotOutputName, prefcriterion, metarules, superResults, subOutDir)
outputFunctions.generateNormalisedTables(evdb, outputFile, dotOutputName)

dotFullPath = subOutDir + dotOutputName
print "Calling dot on: ", dotFullPath
subprocess.call(['dot', '-Tsvg', '-O', dotFullPath], shell = True)
imgOutputName = subOutDir  + dotOutputName + '.svg'
print "SVG Outputname ", imgOutputName

os.startfile(os.path.abspath(imgOutputName))
os.startfile(os.path.abspath(outputFile))


#print "\n\n\n"

#normalTable = normalizeTable.makeTable(evdb)

#for x in normalTable:
#    print x.get('Stage','unknown')

#for x in normalTable:
#    i = x.get('myid','error')
#    print i
#    r = getFullRow(i,evdb)
#    print r.get('RecruitmentArea','unknown')
    
 


# In generateSuperGraph module, the subtable for each
# treatment pair is identified. Then the normalized version
# is produced. So the numbering for each item of normalized table
# is just for that subtable. 

#################################################
#################################################
#################################################
#################################################

